package org.bbl.db.mybatis.mysql;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author biniamg
 * @since 10/31/12
 */
class MyDBConnectionManager {
    private final File resource;
    private final String env;
    private final SqlSessionFactory sqlSessionFactory;
    private final Logger logger = Logger.getLogger(MyDBConnectionManager.class);

    public MyDBConnectionManager(File resource, String env) {
        this.resource = resource;
        this.env = env;
        sqlSessionFactory = buildSessionFactory();
    }

    private SqlSessionFactory buildSessionFactory() {
        try {
            return new SqlSessionFactoryBuilder().build(new FileInputStream(resource), env);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
}
